package com.example.fund;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.example.fund.amplitude.Amplitude;
import com.example.fund.amplitude.TimePeriod;
import com.example.fund.info.FundInfo;
import com.example.fund.util.FundUtil;
import com.example.fund.util.FundUtil1;
import com.example.fund.util.StringUtil;

import android.content.Context;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.util.AttributeSet;  
import android.view.View;  
  
public class VolumeDrawView extends View {  
	
	private ParamInfo paramInfo;
	
	private int bg1 = Color.rgb(233,244,251);
	private int bg2 = Color.rgb(5, 167, 247);
	private int w;
	private int h;
	private int startIndex;
	private int endIndex;
	private BigDecimal min;
	private BigDecimal max;
	
	private static int[] color = Constant.colorSet;
	private static List<String> dateList = FundUtil.getSHIndexDateList();
	private static FundInfo SHInfo = FundUtil.getFundNetValueFromLocalhost("上证", "0000001");
	
    public VolumeDrawView(Context context,AttributeSet set)  
    {  
        super(context,set);
    }
    
    /**
     * 重写该方法,进行绘图  
     */
    @Override  
    protected void onDraw(Canvas canvas)  
    {  
        super.onDraw(canvas);
        //把整张画布绘制成白色  
        canvas.drawColor(bg1);  
        Paint paint = new Paint();  
        //去锯齿  
        paint.setAntiAlias(true);  
        paint.setStyle(Paint.Style.FILL); 
        //绘图所需的数据参数
        if(paramInfo==null)
        {
        	return;
        }
        //计算绘图区的高度
        h = calHeight();
        w = calWidth();
        startIndex = getStartIndex();
        endIndex = getEndIndex();
        BigDecimal[] minAndMax = calMinAndMax();
        min = minAndMax[0];
        max = minAndMax[1]; 
        
        //画横线刻度
        drawHorizontal(canvas,paint);
        //画竖线刻度
        drawVertical(canvas,paint);
        //画走势线
//        drawChartsLine(canvas,paint);
        //画折线
//        drawSimpleLine(canvas,paint);
        //画比值线
        drawDiffLine(canvas,paint);
        //画十字线和长按时画竖线
//        drawTenLine(canvas,paint);
        //画上证指数高低点线
 //       drawHighAndlowLine(canvas,paint);
    }
    
    /**
     * 重绘drawView界面
     */
    public void rePaintDrawView(ParamInfo paramInfo)
    {
    	this.paramInfo = paramInfo;
    	this.invalidate();
    }

    /**
     * 画上证指数高低点线
     * @param canvas
     * @param paint
     */
	private void drawHighAndlowLine(Canvas canvas, Paint paint) 
	{
		BigDecimal pRate = new BigDecimal("50.00");
		BigDecimal nRate = new BigDecimal("-40.00");
		if(dateList==null || dateList.size()==0)
		{
			return;
		}
		Map<String, String> SHInfoV = SHInfo.getActualValues();
	//	List<String> amplitudeDateList = TimePeriodUtil.getAmplitudeDateList(db, dateList, pRate, nRate);
		List<String> amplitudeDateList = new TimePeriod(new Amplitude()).getSHAmplitudeDateList(dateList, pRate, nRate);
		paint.setColor(color[13]);
		paint.setStrokeWidth(1.5f); 
		int lastX = -1;
		String lastDate = null;
		int y = 7*h/10;
		for(int i=startIndex;i>endIndex;i--)
		{
			String date = dateList.get(i);
			if(amplitudeDateList.contains(date))
			{
				int x = indexToX(i);
				if(lastX==-1)
				{
					lastX = x;
					lastDate = date;
				}
//				paint.setColor(color[13]);
				String lastValue = SHInfoV.get(lastDate);
				String value = SHInfoV.get(date);
				if(lastValue!=null && value!=null && !lastValue.equals(value))
				{
					if(Double.valueOf(value)>Double.valueOf(lastValue))
					{
						paint.setColor(color[6]);
					}else
					{
						paint.setColor(color[3]);
					}
					canvas.drawLine(lastX, y, x, y, paint);
			//		canvas.drawLine(x, 0, x, h, paint);
					lastX = x;
					lastDate = date;
				}
			}
		}
	}

	/**
     * 画竖线刻度
     * @param canvas
     * @param paint
     */
	private void drawVertical(Canvas canvas, Paint paint)
	{
		String startDate = paramInfo.getStartDate();
		String endDate = paramInfo.getEndDate();

		if(SHInfo==null||startDate==null || endDate==null || dateList==null || dateList.size()==0 || startIndex<=endIndex)
		{
			return;
		}
		TimePeriod timePeriod = new TimePeriod(new Amplitude());
		List<String> shAmpDateList = timePeriod.getSHAmplitudeDateList(dateList, new BigDecimal("10.0"), new BigDecimal("-9.0"));
		List<String> shAmpDateList2 = timePeriod.getSHAmplitudeDateList(dateList, new BigDecimal("50.0"), new BigDecimal("-40.0"));
		List<String> shAmpDateList1 = new LinkedList<String>();
		List<String> shAmpDateList3 = new LinkedList<String>();
		List<String> shAmpDateList4 = new LinkedList<String>();
		List<String> shAmpDateList5 = new LinkedList<String>();
		for(int i=1;i<shAmpDateList2.size();i++)
		{
			if(i%2==1)
			{
				String dateStr = shAmpDateList2.get(i);
				shAmpDateList3.add(dateStr);
			}else if(i%2==0)
			{
				String dateStr = shAmpDateList2.get(i);
				shAmpDateList4.add(dateStr);
			}
		}
		Map<String,Integer> seqMap = new HashMap<String,Integer>();
		double laseValue = -1;
		int p = 0;
		double laseValue1 = -1;
		int p1 = 0;
		Map<String, String> actValues = SHInfo.getVolumes();
		for(int i=0;i<shAmpDateList.size();i++)
		{
			String dateStr = shAmpDateList.get(i);
			if(shAmpDateList3.contains(dateStr))
			{
				String value = actValues.get(dateStr);
				if(value!=null)
				{
					laseValue = Double.valueOf(value);
					p = 1;
				}
			}
			if(laseValue!=-1 && i%2==0)
			{
				String value = actValues.get(dateStr);
				if(value!=null && Double.valueOf(value)<laseValue)
				{
					shAmpDateList1.add(dateStr);
					laseValue = Double.valueOf(value);
					seqMap.put(dateStr, p++);
				}
			}
			if(shAmpDateList4.contains(dateStr))
			{
				String value = actValues.get(dateStr);
				if(value!=null)
				{
					laseValue1 = Double.valueOf(value);
					p1 = 1;
				}
			}
			if(laseValue1!=-1 && i%2==1)
			{
				String value = actValues.get(dateStr);
				if(value!=null && (Double.valueOf(value)-laseValue1)/laseValue1>0.1)
				{
					shAmpDateList5.add(dateStr);
					laseValue1 = Double.valueOf(value);
					seqMap.put(dateStr, p1++);
				}
			}
		}
		
		int space = 1;//日期显示间隔:1.按年2.按季3.按月4.按日
		int daySpace = startIndex-endIndex;
		int monthSpace = getMonthSpace();
		if(monthSpace<=120)
		{
			space=2;
			if(monthSpace<=36)
			{
				if(daySpace>60)
				{
					space = 3;
				}else
				{
					space = 4;
				}
			}
		}
		paint.setTextSize(18);
//        paint.setColor(color[11]);  
        paint.setStrokeWidth(0.5f);
        if(space==1)
        {
			String lastYear = "";
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				if(date.compareTo("2001-06-13")>0)
				{
					if(shAmpDateList1.contains(date) || shAmpDateList5.contains(date))
					{
						if(shAmpDateList1.contains(date))
						{
							paint.setColor(color[1]);
						}else
						{
							paint.setColor(color[6]);
						}
						String year = date.substring(2, 4);
						int x = indexToX(i);
						canvas.drawLine(x, 0, x, h-20, paint);
						int p0 = seqMap.get(date);
						canvas.drawText(String.valueOf(p0), x-7, h-1, paint);
						String month = date.substring(5, 7);
						String day = date.substring(8, 10);
						if(i==startIndex)
						{
							x = x+15;
						}
						paint.setColor(color[11]);
						if(x-lastX<20)
						{
							continue;
						}
						canvas.drawText(year, x-15, h+25, paint);
						canvas.drawText(month, x-15, h+45, paint);
						canvas.drawText(day, x-15, h+65, paint);
						lastX = x;
					}
				}else
				{
					paint.setColor(color[11]);
					String year = date.substring(2, 4);
					if(!lastYear.equals(year))
					{
						int x = indexToX(i);
						if(x>=100)
						{
							canvas.drawLine(x, 0, x, h, paint);
						}
						String month = date.substring(5, 7);
						String day = date.substring(8, 10);
						if(i==startIndex)
						{
							x = x+15;
						}
						if(x-lastX<20)
						{
							continue;
						}
						canvas.drawText(year, x-15, h+25, paint);
						canvas.drawText(month, x-15, h+45, paint);
						canvas.drawText(day, x-15, h+65, paint);
						lastYear = year;
						lastX = x;
					}
				}
			}
        }else if(space==2)
		{
			String lastSeason = "";
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				if(date.compareTo("2001-06-13")>0)
				{
					if(shAmpDateList1.contains(date) || shAmpDateList5.contains(date))
					{
						if(shAmpDateList1.contains(date))
						{
							paint.setColor(color[1]);
						}else
						{
							paint.setColor(color[6]);
						}
						String month = date.substring(5, 7);
						int x = indexToX(i);
						canvas.drawLine(x, 0, x, h-20, paint);
						int p0 = seqMap.get(date);
						canvas.drawText(String.valueOf(p0), x-7, h-1, paint);
						String year = date.substring(2, 4);
						String day = date.substring(8, 10);
						if(i==startIndex)
						{
							x = x+15;
						}
						if(x-lastX<20)
						{
							continue;
						}
						paint.setColor(color[11]);
						canvas.drawText(year, x-15, h+25, paint);
						canvas.drawText(month, x-15, h+45, paint);
						canvas.drawText(day, x-15, h+65, paint);
						lastX = x;
					}
				}else
				{
					paint.setColor(color[11]);
					String month = date.substring(5, 7);
					if("01".equals(month) || "04".equals(month) || "07".equals(month) || "10".equals(month))
					{
						if(!lastSeason.equals(month))
						{
							int x = indexToX(i);
							if(x>=100)
							{
								canvas.drawLine(x, 0, x, h, paint);
							}
							String year = date.substring(2, 4);
							String day = date.substring(8, 10);
							if(i==startIndex)
							{
								x = x+15;
							}
							if(x-lastX<20)
							{
								continue;
							}
							canvas.drawText(year, x-15, h+25, paint);
							canvas.drawText(month, x-15, h+45, paint);
							canvas.drawText(day, x-15, h+65, paint);
							lastSeason = month;
							lastX = x;
						}
					}
				}
			}
		}else if(space==3)
		{
			paint.setColor(color[11]);
			String lastMonth = "";
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				String month = date.substring(5, 7);
				if(!lastMonth.equals(month))
				{
					int x = indexToX(i);
					if(x>=100)
					{
						canvas.drawLine(x, 0, x, h, paint);
					}
					String year = date.substring(2, 4);
					String day = date.substring(8, 10);
					if(i==startIndex)
					{
						x = x+15;
					}
					if(x-lastX<20)
					{
						continue;
					}
					canvas.drawText(year, x-15, h+25, paint);
					canvas.drawText(month, x-15, h+45, paint);
					canvas.drawText(day, x-15, h+65, paint);
					lastMonth = month;
					lastX = x;
				}
			}
		}else
		{
			paint.setColor(color[11]);
			int lastX = 0;
			for(int i=startIndex;i>=endIndex;i--)
			{
				String date = dateList.get(i);
				int x = indexToX(i);
				if(x>=100)
				{
					canvas.drawLine(x, 0, x, h, paint);
				}
				String year = date.substring(2, 4);
				String month = date.substring(5, 7);
				String day = date.substring(8, 10);
				if(i==startIndex)
				{
					x = x+15;
				}
				if(x-lastX<20)
				{
					continue;
				}
				canvas.drawText(year, x-15, h+25, paint);
				canvas.drawText(month, x-15, h+50, paint);
				canvas.drawText(day, x-15, h+75, paint);
				lastX = x;
			}
		}
	}

	/**
     * 画横线刻度
     * @param canvas
     * @param paint
     */
	private void drawHorizontal(Canvas canvas, Paint paint) 
	{
		paint.setColor(color[11]); 
        paint.setStrokeWidth(0.5f); 
		for(int i=0;i<=h;i+=h/10)
		{
			if(i==0)
			{
				canvas.drawLine(0, i+1, w, i+1, paint);
			}else if(i==7*(h/10))
			{
				paint.setStrokeWidth(2f);
				canvas.drawLine(0, i, w, i, paint);
				paint.setStrokeWidth(0.5f);
			}else
			{
				canvas.drawLine(0, i, w, i, paint);
			}
		}
		
        paint.setTextSize(20);
        BigDecimal bigDecimal100 = new BigDecimal("100.00");
        String startDate = paramInfo.getStartDate();
        if(StringUtil.isNullOrEmpty(startDate))
        {
        	return;
        }
        String beginValueStr = SHInfo.getVolumes().get(startDate);
		if(StringUtil.isNullOrEmpty(beginValueStr))
		{
			return;
		}
		BigDecimal beginValue = new BigDecimal(beginValueStr);
		for(int i=0;i<=h;i+=h/10)
		{
			double v = 30.0-2.0*10.0*i/h;
			int k = (int)(v);
			v = k/10.0;
			
			
			BigDecimal value = yTransValue(i);
			BigDecimal rise = BigDecimal.ZERO;
			if(beginValue!=null && value!=null)
			{
				rise = value.subtract(beginValue).multiply(bigDecimal100).divide(beginValue, 2, RoundingMode.HALF_DOWN);
			}
			if(i==0)
			{
				canvas.drawText(String.valueOf(value.intValue()), 5, i+25, paint);
				canvas.drawText(rise+"", 5, i+25+25, paint);
			}else if(i==h)
			{
				canvas.drawText(rise+"", 5, i-5-25, paint);
				canvas.drawText(String.valueOf(value.intValue()), 5, i-5, paint);
			}else
			{
				canvas.drawText(String.valueOf(value.intValue()), 5, i-5, paint);
				canvas.drawText(rise+"", 5, i-2+25, paint);
			}
		}
	}
	
    /**
     * 画走势线
     * @param canvas
     * @param paint
     */
    private void drawChartsLine(Canvas canvas, Paint paint) 
    {
        paint.setStrokeWidth(3); 
		String startDate = paramInfo.getStartDate();
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		if(SHInfo==null || startDate==null 
				|| selectFundInfoList==null || selectFundInfoList.size()>0 || dateList!=null)
		{
			return;
		}
		for(int i=0;i<selectFundInfoList.size();i++)
		{
			FundInfo info = selectFundInfoList.get(i);
			paint.setColor(getIndexColor(info.getNumber())); 
			Map<String, String> actValues = info.getVolumes();//实际净值
			BigDecimal beginValue0 = null;//起始上证实际净值
			BigDecimal beginValue = null;//起始实际净值
			int lastX = -1;
			int lastY = -1;
			for(int j=startIndex;j>=endIndex;j--)
			{
				String dateStr = dateList.get(j);
				String actValueStr = actValues.get(dateStr);
				if(actValueStr!=null)
				{
					BigDecimal actValue = new BigDecimal(actValueStr);
					if(beginValue==null)
					{
						String beginValueStr = SHInfo.getVolumes().get(dateStr);
						beginValue0 = new BigDecimal(beginValueStr);
						beginValue = actValue;
					}
					BigDecimal value = beginValue0.multiply(actValue).divide(beginValue, 2,RoundingMode.HALF_UP);
					int x = indexToX(j);
					int y = valueToY(value);
					if(lastX==-1)
					{
						lastX = x;
					}
					if(lastY==-1)
					{
						lastY = y;
					}
					canvas.drawLine(lastX, lastY, x, y, paint);
					lastX = x;
					lastY = y;

				}
			}
		}
	}
    
    /**
     * 画折线
     * @param canvas
     * @param paint
     */
	private void drawSimpleLine(Canvas canvas, Paint paint) 
	{
		boolean isDisplayDiffLine = paramInfo.isDisplayDiffLine();
		boolean isSimpleLine = paramInfo.isSimpleLine();
		
		if(!isSimpleLine)
		{
			return;
		}
		String positiveRate = paramInfo.getPositiveRate();
		String negativeRate = paramInfo.getNegativeRate();
		if(positiveRate==null || positiveRate.trim().length()==0 || negativeRate==null || negativeRate.trim().length()==0)
		{
			return;
		}
		paint.setStrokeWidth(3); 
		//计算基金波动涨幅值
		double pRate = Double.valueOf(positiveRate);
		double nRate = Double.valueOf(negativeRate);
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		if(selectFundInfoList!=null && selectFundInfoList.size()>0 && dateList!=null)
		{
			
			int size = isDisplayDiffLine?1:selectFundInfoList.size();
			for(int i=0;i<size;i++)
			{
				FundInfo info = selectFundInfoList.get(i);
				paint.setColor(getIndexColor(info.getNumber()));
				Map<String,String> amplitudeMap = new Amplitude().getAmplitudeDateMap(info, dateList, pRate, nRate);
				int lastX = -1;
				int lastY = -1;
				BigDecimal beginValue0 = null;
				BigDecimal beginValue = null;//起始实际净值
				for(int j=startIndex;j>=endIndex;j--)
				{
					String dateStr = dateList.get(j);
					String actValueStr = info.getVolumes().get(dateStr);
					if(actValueStr!=null)
					{
						BigDecimal actValue = new BigDecimal(actValueStr);
						if(beginValue==null)
						{
							String beginValueStr = SHInfo.getVolumes().get(dateStr);
							beginValue0 = new BigDecimal(beginValueStr);
							beginValue = actValue;
						}
						if(amplitudeMap.keySet().contains(dateList.get(j)))
						{
							BigDecimal value = beginValue0.multiply(actValue).divide(beginValue, 2,RoundingMode.HALF_UP);
							String rise = amplitudeMap.get(dateList.get(j));
							
							int x = indexToX(j);
							int y = valueToY(value);
							if(lastX==-1)
							{
								lastX = x;
							}
							if(lastY==-1)
							{
								lastY = y;
							}
							canvas.drawLine(lastX, lastY, x, y, paint);
							
							lastX = x;
							lastY = y;
							if(w-x<30)
							{
								x = x-60;
							}else
							{
								x = x-17;
							}
							if(y<30)
							{
								y = y+30;
							}
							if(x>10)
							{
								paint.setStrokeWidth(1);
								if(rise.contains("-"))
								{
									paint.setColor(color[3]);
									y = y+20;
									canvas.drawText(rise, x, y, paint);
									paint.setColor(getIndexColor(info.getNumber()));
								}else
								{
									paint.setColor(color[6]);
									canvas.drawText(rise, x, y, paint);
									paint.setColor(getIndexColor(info.getNumber()));
								}
							}
							paint.setStrokeWidth(3);
						}
					}
				}
			}
		}
	}
	
	/**
	 * 画比值线 
	 * @param canvas
	 * @param paint
	 */
	private void drawDiffLine(Canvas canvas, Paint paint) 
	{
		boolean isDisplayDiffLine = paramInfo.isDisplayDiffLine();
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		if(selectFundInfoList==null || selectFundInfoList.size()<=1 || !isDisplayDiffLine || dateList==null || SHInfo==null)
		{
			return;
		}
		
		String positiveRate = paramInfo.getPositiveRate();
		String negativeRate = paramInfo.getNegativeRate();
		if(positiveRate==null || positiveRate.trim().length()==0 || negativeRate==null || negativeRate.trim().length()==0)
		{
			return;
		}
		//计算基金波动涨幅值
		double pRate = Double.valueOf(positiveRate);
		double nRate = Double.valueOf(negativeRate);
		
		FundInfo cloneInfo = new FundInfo();
		Map<String, String> amplitudeDateMap = null;
		if(selectFundInfoList.size()==2)
		{
			FundInfo info0 = selectFundInfoList.get(0);
			FundInfo info1 = selectFundInfoList.get(1);
			cloneInfo.setName(info1.getName());
			cloneInfo.setNumber(info1.getNumber());
			Map<String,String> actValues = cloneInfo.getVolumes();
			Map<String,String> actValues0 = info0.getVolumes();
			Map<String,String> actValues1 = info1.getVolumes();
			for(int i=0;i<dateList.size();i++)
			{
				String dateStr = dateList.get(i);
				String actValue0 = actValues0.get(dateStr);
				String actValue1 = actValues1.get(dateStr);
				if(actValue0!=null && actValue1!=null)
				{
					double actValue = Double.valueOf(actValue1)/Double.valueOf(actValue0);
					actValues.put(dateStr, String.valueOf(actValue));
				}
			}
		//	amplitudeDateMap = FundUtil.getAmplitudeDateMap(cloneInfo, dateList, 10.00d, -9.09d);
			amplitudeDateMap = new Amplitude().getAmplitudeDateMap(cloneInfo, dateList, pRate, nRate);
		}
		
		paint.setStrokeWidth(3);
		
		FundInfo refFund = selectFundInfoList.get(0);
		Map<String,Map<String, String>> diffMap = new HashMap<String,Map<String, String>>();
		for(int i=1;i<selectFundInfoList.size();i++)
		{
			FundInfo info = selectFundInfoList.get(i);
			String number = info.getNumber();
			
			Map<String,String> diffValues = new HashMap<String,String>();
			diffMap.put(number, diffValues);
			BigDecimal minValue = BigDecimal.ONE;
			
			BigDecimal beginValue0 = null;
			BigDecimal beginValue1 = null;//起始实际净值
			BigDecimal beginValue2 = null;//起始实际净值
			for(int j=startIndex;j>=endIndex;j--)
			{
				String dateStr = dateList.get(j);

				String actValueStr1 = refFund.getVolumes().get(dateStr);
				String actValueStr2 = info.getVolumes().get(dateStr);
				if(actValueStr1!=null && actValueStr2!=null)
				{
					BigDecimal actValue1 = new BigDecimal(actValueStr1);
					BigDecimal actValue2 = new BigDecimal(actValueStr2);
					if(beginValue1==null)
					{
						String beginValueStr0 = SHInfo.getVolumes().get(dateStr);
						beginValue0 = new BigDecimal(beginValueStr0);
						beginValue1 = actValue1;
						beginValue2 = actValue2;
					}
					BigDecimal value1 = beginValue0.multiply(actValue1).divide(beginValue1, 2,RoundingMode.HALF_UP);
					BigDecimal value2 = beginValue0.multiply(actValue2).divide(beginValue2, 2,RoundingMode.HALF_UP);
					BigDecimal difValue = value2.divide(value1, 4, RoundingMode.HALF_UP);
					if(difValue.compareTo(minValue)<0)
					{
						minValue = difValue;
					}
					diffValues.put(dateStr, difValue.toString());
				}
			}
			if(minValue.compareTo(BigDecimal.ONE)<0)
			{
				BigDecimal tempValue = BigDecimal.ONE.divide(minValue, 4,RoundingMode.HALF_UP);
				for(String dateStr:diffValues.keySet())
				{
					String valueStr = diffValues.get(dateStr);
					diffValues.put(dateStr, new BigDecimal(valueStr).multiply(tempValue).toString());
				}
			}
			
		}
		
		for(int i=1;i<selectFundInfoList.size();i++)
		{
			FundInfo info = selectFundInfoList.get(i);
			String number = info.getNumber();
			
			paint.setColor(getIndexColor(number));
			int lastX1 = -1;
			float lastY1 = -1;
			int lastX = -1;
			float lastY = -1;
//			BigDecimal beginValue0 = null;
//			BigDecimal beginValue1 = null;//起始实际净值
//			BigDecimal beginValue2 = null;//起始实际净值
			for(int j=startIndex;j>=endIndex;j--)
			{
				String dateStr = dateList.get(j);

				String actValueStr1 = refFund.getVolumes().get(dateStr);
				String actValueStr2 = info.getVolumes().get(dateStr);
				if(actValueStr1!=null && actValueStr2!=null)
				{
//					BigDecimal actValue1 = new BigDecimal(actValueStr1);
//					BigDecimal actValue2 = new BigDecimal(actValueStr2);
//					if(beginValue1==null)
//					{
//						String beginValueStr0 = SHInfo.getActualValues().get(dateStr);
//						beginValue0 = new BigDecimal(beginValueStr0);
//						beginValue1 = actValue1;
//						beginValue2 = actValue2;
//					}
//					BigDecimal value1 = beginValue0.multiply(actValue1).divide(beginValue1, 2,RoundingMode.HALF_UP);
//					BigDecimal value2 = beginValue0.multiply(actValue2).divide(beginValue2, 2,RoundingMode.HALF_UP);
					Map<String, String> diffValues = diffMap.get(number);
					if(diffValues==null || diffValues.size()==0)
					{
						continue;
					}
					String difValue = diffValues.get(dateStr);
					if(difValue==null || difValue.trim().length()==0)
					{
						continue;
					}
					float y = (3.0f-((new BigDecimal(difValue)).floatValue()))*h/2.00f;
					int x = indexToX(j);
					if(lastX==-1)
					{
						lastX = x;
						lastY = y;
					}
					canvas.drawLine(lastX, lastY, x, y, paint);
					lastX = x;
					lastY = y;
					
					if(amplitudeDateMap!=null)
					{
						if(lastX1==-1)
						{
							lastX1 = x;
							lastY1 = y;
						}
						if(amplitudeDateMap.containsKey(dateStr))
						{
							canvas.drawLine(lastX1, lastY1, x, y, paint);
							lastX1 = x;
							lastY1 = y;
							String rise = amplitudeDateMap.get(dateStr);
							rise = new BigDecimal(1.00d+Double.valueOf(rise)/100.00d).setScale(2, RoundingMode.HALF_UP).toString();
							
							if(w-x<30)
							{
								x = x-60;
							}else
							{
								x = x-17;
							}
							if(y<30)
							{
								y = y+30;
							}
							if(x>10)
							{
								paint.setStrokeWidth(1);
								if(Double.valueOf(rise)<1)
								{
									paint.setColor(color[3]);
									y = y+20;
									canvas.drawText(rise, x, y, paint);
									paint.setColor(getIndexColor(number));
								}else
								{
									paint.setColor(color[6]);
									canvas.drawText(rise, x, y-5, paint);
									paint.setColor(getIndexColor(number));
								}
								paint.setStrokeWidth(3);
							}
						}
					}
				}
			}
		}
	}
	
    /**
     * 画十字线、画长按时的竖线
     * @param canvas
     * @param paint
     */
	private void drawTenLine(Canvas canvas, Paint paint) 
	{
//		paint.setColor(color[8]);  
//        paint.setStrokeWidth(2); 
//        float x = paramInfo.getX();
 //       float y = paramInfo.getY();
//		canvas.drawLine(x, 0, x, h, paint);
//		canvas.drawLine(0, y, w, y, paint);
		//画长按时的竖线
/*		List<Float> coordinateXList = paramInfo.getCoordinateXList();
		if(coordinateXList!=null)
		{
			for(int i=0;i<coordinateXList.size();i++)
			{
				float ax = coordinateXList.get(i);
				canvas.drawLine(ax, 0, ax, h, paint);
			}
			
		}*/
	}
	
	/**
	 * 计算绘图区的宽度
	 * @return
	 */
    private int calWidth() {
    	int w = this.getWidth()-7;
   // 	w = w-25*3-w%10;
		return w;
	}
	
	/**
	 * 计算绘图区的高度
	 * @return
	 */
	private int calHeight()
	{
		int h = this.getHeight();
		h = h-30*3-h%10;
		return h;
	}

	
	/**
	 * 计算两个交易日期之间的交易日期数
	 * @return
	 */
	private int getMonthSpace()
	{
		String startDate = paramInfo.getStartDate();
		String endDate = paramInfo.getEndDate();
		int n =0;
		Set<String> monthSet = new HashSet<String>();
		if(startDate!=null && endDate!=null && dateList!=null && dateList.size()>0)
		{
			for(int i=0;i<dateList.size();i++)
			{
				String date = dateList.get(i);
				if(endDate.equals(date))
				{
					for(int j=i;j<dateList.size();j++)
					{
						String date1 = dateList.get(j);
						monthSet.add(date1.substring(0, 7));
						if(startDate.equals(date1))
						{
							n = monthSet.size();
							break;
						}
					}
					break;
				}
			}
		}
		return n;
	}
	
	/**
	 * 获取开始日期下标
	 * @return
	 */
	private int getStartIndex()
	{
		String startDate = paramInfo.getStartDate();
		int n = 0;
		if(startDate!=null && dateList!=null && dateList.size()>0)
		{
			for(int i=0;i<dateList.size();i++)
			{
				String date = dateList.get(i);
				if(startDate.equals(date))
				{
					n = i;
					break;
				}
			}
		}
		return n;
	}
	
	/**
	 * 获取结束日期下标
	 * @return
	 */
	private int getEndIndex()
	{
		String endDate = paramInfo.getEndDate();
		int n = 0;
		if(endDate!=null  && dateList!=null && dateList.size()>0)
		{
			for(int i=0;i<dateList.size();i++)
			{
				String date = dateList.get(i);
				if(endDate.equals(date))
				{
					n = i;
					break;
				}
			}
		}
		return n;
	}
	
	/**
	 * 日期下标转X坐标
	 * @param index
	 * @return
	 */
	private int indexToX(int i)
	{
		return ((startIndex-i)*w)/(startIndex-endIndex);
	}

	/**
	 * 涨幅值转Y坐标 
	 * @param rise
	 * @return
	 */
	private int valueToY(BigDecimal rise)
	{
		if(max.compareTo(min)==0)
		{
			return 0;
		}
		int y = (max.subtract(rise).multiply(new BigDecimal(h)).divide(max.subtract(min),2,RoundingMode.HALF_UP)).intValue();
		return y;
	}
	
	/**
	 * 坐标Y值转换成对应涨幅
	 * @param max
	 * @param min
	 * @param i
	 */
	private BigDecimal yTransValue(int i) 
	{
		BigDecimal rise = max.subtract(max.subtract(min).multiply(new BigDecimal(i)).divide(new BigDecimal(h),2,RoundingMode.HALF_UP));
		return rise;
	}
	
	/**
	 * 计算最小最大值
	 * @return
	 */
	private BigDecimal[] calMinAndMax()
	{
		List<FundInfo> selectFundInfoList = paramInfo.getSelectFundInfoList();
		BigDecimal min = BigDecimal.ZERO;//找出最小值
		BigDecimal max = BigDecimal.ZERO;//找出最大值
		if(selectFundInfoList==null || selectFundInfoList.size()==0
				|| dateList==null || dateList.size()==0)
		{
			return new BigDecimal[]{min,max};
		}
		
		String startDate = paramInfo.getStartDate();
		String beginValueStr = SHInfo.getVolumes().get(startDate);
		if(StringUtil.isNullOrEmpty(beginValueStr))
		{
			return new BigDecimal[]{min,max};
		}
		BigDecimal beginValue0 = new BigDecimal(beginValueStr);
		min = beginValue0;
		max = beginValue0;
		for(int i=0;i<selectFundInfoList.size();i++)
		{
			FundInfo info = selectFundInfoList.get(i);
			Map<String, String> volumes = info.getVolumes();//
			BigDecimal beginValue = null;//
			for(int j=startIndex;j>=endIndex;j--)
			{
				String dateStr = dateList.get(j);
				String volumeStr = volumes.get(dateStr);
				if(volumeStr!=null && Double.valueOf(volumeStr)>0)
				{
					BigDecimal volume = new BigDecimal(volumeStr);
					if(beginValue==null)
					{
						beginValueStr = SHInfo.getVolumes().get(dateStr);
						if(StringUtil.isNullOrEmpty(beginValueStr))
						{
							continue;
						}
						beginValue0 = new BigDecimal(beginValueStr);
						beginValue = volume;
					}
					BigDecimal rise = beginValue0.multiply(volume).divide(beginValue, 2,RoundingMode.HALF_UP);
					if(min.compareTo(rise)==1)
					{
						min = rise;
					}
					if(max.compareTo(rise)==-1)
					{
						max = rise;
					}
				}
			}
		}

		return new BigDecimal[]{min,max};
	}
	
	private int getIndexColor(String number)
	{
		Integer c = FundUtil1.getIndexColor(number);

		if(c==null)
		{
			c = color[0];
		}
		return c;
	}
	
	public int getW() {
		return w;
	}
	public int getH() {
		return h;
	}
}  
